package com.automannn.springPractice.applicationContext.bean.factory.impl;

import com.automannn.springPractice.applicationContext.User;
import com.automannn.springPractice.applicationContext.bean.factory.UserFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 默认用户的工厂
 *
 * @author huangk
 * @date 2022/08/03
 */
public class DefaultUserFactory implements UserFactory, InitializingBean, DisposableBean {
    @PostConstruct
    public void init() {
        User user = getUser();
        user.setName("@PostConstruct实现");
        System.out.println("@PostConstruct注解实现，初始化后，进入" + user.toString());
    }

    /**
     * 在属性设置
     * 自定义InitializingBeanapi设置
     *
     * @throws Exception 异常
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        User user = getUser();
        user.setName("afterPropertiesSet");
        System.out.println("实现 InitializingBean api设置" + user.toString());
    }

    /**
     * 自定义初始化
     */
    public void customInit() {
        User user = getUser();
        user.setName("customInit");
        System.out.println("customInit进入自定义初始化方法" + user.toString());
    }

    /**
     * 毁坏
     */
    @PreDestroy
    public void annotationsDestory(){
        System.out.println("注解销毁bean");
    }

    /**
     * 毁坏
     */
    public void customDestory(){
        System.out.println("自定义销毁方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean destroy");
    }

    @Override
    public void finalize() throws Throwable {
        System.out.println("spring bean进行垃圾回收");
    }
}
